#include "QSQLControl.h"

QSQLControl::QSQLControl()
{

}

void QSQLControl::ReLoad()
{
    _Qdb.removeDatabase(_Qdb.databaseName());
    _Qdb.setDatabaseName(_db.data());
}

void QSQLControl::ConnectToDB()
{
    static int connectName = 0;
    _Qdb = QSqlDatabase::addDatabase(_db_type.data(),QString::number(connectName));
    _Qdb.setDatabaseName(_db.data());
///     密码功能暂时先保留
//    _Qdb.setUserName(userName.data());
//    _Qdb.setPassword(password.data());
    _Qdb.open() ? undo() : throw MU_EXPS::EXP_SQL_FALSE;

    connectName++;
    // pushExp(MU_Messages::SQL,MU_EXPS::EXP_SQL_FALSE,MU_ERROR_MODULE_SQL)
}





void QSQLControl::DisConnect()
{
    _Qdb.removeDatabase(_Qdb.databaseName());
}

void QSQLControl::Exec(std::string _query)
{
    QSqlQuery query(_query.data(), _Qdb);
    // query.exec();
}

void QSQLControl::insert(QString _query, QStringList list)
{
    QSqlQuery query(_query, _Qdb);
    foreach (auto var, list) {
        query.addBindValue((QVariant)var);
    }
    query.exec();

    qDebug()<<query.lastError().text();
}



QVariantList QSQLControl::Get(std::string _query)
{
    QSqlQuery query(_query.data(), _Qdb);
    try{
        query.exec();
        QVariantList ret;
        QSqlRecord rec = query.record();
        while (query.next()) {
            for(int i = 0;i<rec.count();i++){
                ret.push_back( query.value(i) );
            }

        }
        if(QSqlError::NoError != query.lastError().type()){
            qDebug()<<"[QSql-Error]:"<<query.lastError().text();
            qDebug()<<"[QSql-Error]:"<<query.lastError().type();
            throw MU_EXPS::EXP_SQL_READ;
        }
        // pushExp(MU_Messages::SQL,MU_EXPS::EXP_SQL_READ,MU_ERROR_MODULE_SQL)
        return  ret;
    }catch(...){
        qDebug()<<"Unkonw Error -Qt";
    }

    QVariantList list;
    list<<1<<2<<3<<4<<5;
    return list;

}

QVariantList QSQLControl::Get(std::string tableName,std::initializer_list<std::string> result, std::initializer_list<std::string> condition)
{
    std::string _query = "select ";
    int i = 0;
    int count = result.size();
    for(auto& str:result)
    {
        _query += str + " ";
        i++;
        if(i != count)
        {
            _query += ",";
        }
    }
    _query += "from " + tableName + " where ";
    i = 0;
    count = condition.size();
    for(auto& con:condition)
    {
        if(i%2 == 0)
            _query += con + " ";
        else
            _query += "= " + con + " ";
        i++;
        if(i%2 == 0 && i < count)
            _query += "and ";
    }

    if(!_query.empty())
    {
        _query += ";";
    }
    QSqlQuery query(_query.data(), _Qdb);

    try{
        query.exec();
        QVariantList ret;
        while (query.next()) {
            for(auto& str:result){
                ret.push_back( query.value(QString::fromStdString(str)) );
            }
        }
        if(QSqlError::NoError != query.lastError().type()){
            qDebug()<<"[QSql-Error]:"<<query.lastError().text();
            throw MU_EXPS::EXP_SQL_READ;
        }
        // pushExp(MU_Messages::SQL,MU_EXPS::EXP_SQL_READ,MU_ERROR_MODULE_SQL)
        return  ret;
    }catch(...){
        qDebug()<<"[QSql-Error]:Unkonw Error!";
    }

    QVariantList list;
    list<<1<<2<<3<<4<<5;
    return list;
}

